<?php

namespace Home\Controller;


use Org\SignatureHelper;
use Think\Controller;


class IndexController extends Controller
{
    public function index()
    {
        $this->display();
    }

    public function about()
    {
        $this->display();
    }

    public function getVerifyCode()
    {
        $tel = I('get.tel');

        //1.获取验证码
        $verifyCode = str_pad(mt_rand(10, 999999), 6, "0", STR_PAD_BOTH);
        //2.发送验证码.
        $result = $this->sendSms($tel, $verifyCode);
        if ($result->Code == 'OK') {
            session($tel, $verifyCode);
            //  4.返回信息
            echo json_encode(array('code' => 0, 'msg' => '发送成功'));
        } else {
            echo json_encode(array('code' => 1, 'msg' => $result->Message));
        }
    }

    private function sendSms($Number, $Code)
    {
        $params = array();
        // *** 需用户填写部分 ***
//      必填：是否启用https
        $security = false;
        //  必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
        $accessKeyId = "LTAI5Y2eoBfW2N2Q";
        $accessKeySecret = "gLACsgg49Xqo9GNfwiFZi0JWLdWGxz";
        //  必填: 短信接收号码
        $params["PhoneNumbers"] = $Number;

        //  必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
        $params["SignName"] = "重庆椿林";

        //  必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $params["TemplateCode"] = "SMS_164240162";

        //  可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
        $params['TemplateParam'] = Array(
            "code" => $Code,
//        "product" => "阿里通信"
        );

        //  可选: 设置发送短信流水号
        $params['OutId'] = "12345";

        //  可选: 上行短信扩展码, 扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段
        $params['SmsUpExtendCode'] = "1234567";


        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }

        // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
        $helper = new SignatureHelper();

        // 此处可能会抛出异常，注意catch
        $content = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "SendSms",
                "Version" => "2017-05-25",
            )),
            $security
        );

        return $content;
    }

    public function insertUser()
    {
        //1.判断验证码
        $verifyCode = I('get.verifyCode');
        $tel = I('get.tel');

//        echo json_encode(session($tel));
//        exit(0);


        if ($verifyCode == session($tel)) {
//            //2.保存信息
            $postData = I('get.');
            $data = array(
                'name' => $postData['username'],
                'typeName' => $postData['typeName'],
                'tel' => $postData['tel'],
                'content' => $postData['content'],
            );
            M()->startTrans();
            $count=M('message')->where(array(
                'typeName' => $postData['typeName'],
                'name' => $postData['username'],
                'tel' => $postData['tel']
            ))->count();

            if($count>0){
                M()->rollback();
                echo json_encode(array('code' => 1, 'msg' => '请勿重复提交'));
            }else{
                $result = M('message')->add($data);
                if (false === $result) {
                    M()->rollback();
                    echo json_encode(array('code' => 1, 'msg' => '系统出错'));
                } else {
                    M()->commit();
                    echo json_encode(array('code' => 0, 'msg' => '提交成功'));
                }
            }

        } else {
            echo json_encode(array('code' => 1, 'msg' => '验证码错误'));
        }
    }

}